<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch, computed } from 'vue'
import * as echarts from 'echarts'

interface SensorData {
  id: string
  name: string
  value: number
  timestamp: string
}

const props = defineProps<{
  sensorData: SensorData[]
  timestamp: string
}>()

const chartDom = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

// 处理传感器数据
const chartData = computed(() => {
  if (!props.sensorData?.length) {
    return {
      nitrogen: 0,
      phosphorus: 0,
      potassium: 0
    }
  }

  const findValue = (name: string) => {
    const sensor = props.sensorData.find(s => s.name.includes(name))
    return sensor?.value || 0
  }

  return {
    nitrogen: findValue('氮'),
    phosphorus: findValue('磷'),
    potassium: findValue('钾')
  }
})

// 更新图表
const updateChart = () => {
  if (!chart) return

  const option = {
    backgroundColor: 'transparent',
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['氮含量', '磷含量', '钾含量'],
      textStyle: {
        color: '#4fd2dd'
      },
      top: 10
    },
    grid: {
      left: '10%',
      right: '10%',
      bottom: '10%',
      top: '20%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['当前值'],
      axisLine: {
        lineStyle: {
          color: '#4fd2dd'
        }
      },
      axisLabel: {
        color: '#4fd2dd',
        fontSize: 14
      }
    },
    yAxis: {
      type: 'value',
      name: 'MG/KG',
      nameTextStyle: {
        color: '#4fd2dd',
        fontSize: 14,
        padding: [0, 30, 0, 0]
      },
      axisLine: {
        lineStyle: {
          color: '#4fd2dd'
        }
      },
      axisLabel: {
        color: '#4fd2dd',
        fontSize: 12
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(79,210,221,0.1)'
        }
      }
    },
    series: [
      {
        name: '氮含量',
        type: 'bar',
        barWidth: '20%',
        data: [chartData.value.nitrogen],
        itemStyle: {
          color: '#4fd2dd'
        },
        label: {
          show: true,
          position: 'top',
          color: '#fff',
          fontSize: 14
        }
      },
      {
        name: '磷含量',
        type: 'bar',
        barWidth: '20%',
        data: [chartData.value.phosphorus],
        itemStyle: {
          color: '#ffc93c'
        },
        label: {
          show: true,
          position: 'top',
          color: '#fff',
          fontSize: 14
        }
      },
      {
        name: '钾含量',
        type: 'bar',
        barWidth: '20%',
        data: [chartData.value.potassium],
        itemStyle: {
          color: '#ff6e76'
        },
        label: {
          show: true,
          position: 'top',
          color: '#fff',
          fontSize: 14
        }
      }
    ]
  }
  
  chart.setOption(option)
}

// 监听数据变化
watch(() => props.sensorData, updateChart, { deep: true })

const initChart = () => {
  if (chartDom.value) {
    chart = echarts.init(chartDom.value)
    updateChart()
  }
}

const resizeHandler = () => {
  chart?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', resizeHandler)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', resizeHandler)
  chart?.dispose()
})
</script>

<template>
  <div ref="chartDom" class="npk-chart"></div>
</template>

<style scoped>
.npk-chart {
  width: 100%;
  height: 100%;
  padding: 20px;
}
</style> 